package com.xumz.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.xumz.model.RecordManage;
import com.xumz.model.Student;
import com.xumz.service.RecordService;
import com.xumz.service.StudentService;



@Controller
public class RecordController {
	@Autowired
	RecordService service;
	@Autowired
	StudentService studentService;
	
	public RecordController() {
		System.out.println("RecordController");
	}
	@RequestMapping("/index")
	public String indexdo(HttpServletRequest request,Model model){
		List<RecordManage> list = service.selectAllRecordManage();
		model.addAttribute("list",list);
		return "teacher/index";
	}
	@RequestMapping(value = "/index1/{num}",method=RequestMethod.GET)
	public String indexdo(HttpServletRequest request,Model model,@PathVariable("num") String num
			) throws UnsupportedEncodingException {
		List<RecordManage> list = service.selectAllRecordManage(num);
		model.addAttribute("list",list);
		return "teacher/index";
	}
	@RequestMapping("/recordcrawler")
	public String recordcrawler(Model model){
		return "teacher/admin/record/add";
	}
	@RequestMapping("/recordcrawlerPost")
	public String recordcrawlerPost(@RequestParam("blogUrl")String blogUrl,Model model){
		service.insertList(service.getCrawler(blogUrl,null));
		return "teacher/admin/record/succ";
	}
	@RequestMapping("/recordIndex")
	public String recordIndex(HttpServletRequest request,Model model){	
		String name=(String) request.getSession().getAttribute("name");
		List<RecordManage> list=service.selectAllRecordManageByTeacherName(name);
		System.out.println(list);
		model.addAttribute("list",list);
		return "teacher/admin/record/index";
	}
	
	@RequestMapping("/recordSearchPost")
	public String recordSearchPost(HttpServletRequest request,Model model,@RequestParam("studentSearch")String studentSearch,
			@RequestParam("keyWordSearch")String keyWordSearch){	
		String name=(String) request.getSession().getAttribute("name");
		List<RecordManage> list=service.selectAllRecordManageByTeacherName(name);
		if (!studentSearch.equals("")) {
			list=service.selectRecordManageByOwer(list,studentSearch);
			model.addAttribute("studentSearch",studentSearch);
		}
		if (!keyWordSearch.equals("")) {
			list=service.selectRecordManageByKeyWord(list, keyWordSearch);
			model.addAttribute("keyWordSearch",keyWordSearch);
		}
		model.addAttribute("list",list);
		return "teacher/admin/record/index";
	}
	@RequestMapping(value = "/recordFromStudent/{stuId}",method=RequestMethod.GET)
	public String recordFromStudent(Model model,@PathVariable("stuId") String stuId) throws UnsupportedEncodingException{
		String studentId=new String(stuId.getBytes("ISO-8859-1"), "UTF-8");
		Student student=studentService.findStudentById(studentId);
		model.addAttribute("studentSearch", student.getStuName());
		List<RecordManage> list=service.selectByStudentId(studentId);
		model.addAttribute("list",list);
		return "teacher/admin/record/index";
	}
	
}
